<?php

function find_form() {
    $result = new Deferred();
    //start body
    $curl = new multiCURL('http://www.google.com/');
    $curl->addEventListener('onLoad', function(Event $event) use ($result) {
        $dom = new DOMDocument('1.0', 'urf-8');
        @$dom->loadHTML($event->target->result);
        $xpath = new DOMXPath($dom);
        $forms = $xpath->query('//form');
        if(1 != $forms->length) {
            echo 'Form error on index page';
            $result->errback(null);
            throw new DieException('Form error on index page');
        }
        $form = $forms->item(0);
        $form_action = $form->getAttribute('action');
        $inputs = $xpath->query('//input', $form);
        for($i = 0, $submit_count = 0; $i < $inputs->length; $i++) {
            $type = $inputs->item($i)->getAttribute('type');
            $name = $inputs->item($i)->getAttribute('name');
            $value = $inputs->item($i)->getAttribute('value');
            if(('submit' == $type && ++$submit_count == 1) || true) {
                $form_data[$name] = $value;
            }
        }


        $result->callback(array(
            'form_action' => $form_action,
            'form_data'   => $form_data
        ));
    });
    $queue->addTask($curl);
    //end body
    return $result;
}
function submit_form($form_info, $query) {
    $result = new Deferred();
    if(!is_a($form_info, 'Deferred')) {
        $start = new Deferred();
        $start->callback($form_info);
    } else {
        $start = $form_info;
    }
    $start->addCallback(function($data) use ($result) {
        //start body
        
        //end body
    });
    return $result;
}





//==============================================================================
function qwe($data) {
    $result = new Deferred();
    if(!is_a($data, 'Deferred')) {
        $start = new Deferred();
        $start->callback($data);
    } else {
        $start = $data;
    }
    $start->addCallback(function($data) use ($result) {
        //start body
        
        //end body
    });
    return $result;
}